<?php

class Marticle extends TST_Model {

    protected $table_articles = "articles";
    protected $table_category = "category";
    protected $selected_columns = "*";
    protected $selected_columns_cart = "id,product_price,product_name,product_code";

    function Marticle() {
        parent::TST_Model();
    }

    // lấy dữ liệu theo từng phần
    function list_all($category_url = null, $number = null, $offset = null, $status = AppContants::ACTIVE_STATUS) {
        $sql = "SELECT a.id, a.friendly_url, a.full_description, a.name, a.icon_image, a.short_description                                   
                FROM {$this->table_articles} a INNER JOIN {$this->table_category} c ON a.category_id = c.id
                WHERE a.status = ?
                AND   c.friendly_url = ? 
                ORDER BY a.in_order, a.name 
                LIMIT ?, ? ";
        return $this->query_array($sql, array($status, $category_url, $number, $offset));
    }

    public function getArticleDetailByCategoryUrl($category_url = null, $status = AppContants::ACTIVE_STATUS) {
        $sql = "SELECT a.id, a.friendly_url, a.full_description, a.name, a.icon_image                                   
                    FROM {$this->table_articles} a INNER JOIN {$this->table_category} c ON a.category_id = c.id
                    WHERE a.status = ?
                    AND   c.friendly_url = ? ";
        return $this->query_row($sql, array($status, $category_url));
    }

    public function getArticleDetailByFriendlyUrl($friendly_url = null, $status = AppContants::ACTIVE_STATUS) {
        $sql = "SELECT a.id, a.friendly_url, a.full_description, a.name, a.icon_image                                   
                    FROM {$this->table_articles} a INNER JOIN {$this->table_category} c ON a.category_id = c.id
                    WHERE a.status = ?
                    AND   a.friendly_url = ? ";
        return $this->query_row($sql, array($status, $friendly_url));
    }

    public function getAllArticleByCategoryUrl($category_url = null, $status = AppContants::ACTIVE_STATUS) {
        $sql = "SELECT a.id, a.friendly_url, a.full_description, a.name, a.icon_image, a.short_description                                   
                FROM {$this->table_articles} a INNER JOIN {$this->table_category} c ON a.category_id = c.id
                WHERE a.status = ?
                AND   c.friendly_url = ? 
                ORDER BY a.in_order, a.name ";
        return $this->query_array($sql, array($status, $category_url));
    }

    public function getAllArticleByCategoriesID1($language, $id, $status) {
        $sql = "SELECT a.*, c.friendly_url AS cate_url, p.friendly_url AS pro_url
FROM article a INNER JOIN product p ON a.product_id = p.id INNER JOIN category c ON p.category_id = c.id
WHERE a.language = ?
AND a.status = ?
AND c.status = ?
AND p.status = ?
AND a.category_id = ?
AND a.friendly_url NOT LIKE 'request-a-quote'
ORDER BY a.in_order, a.name ";
        return $this->query_array($sql, array($language, $status, $status, $status, $id));
    }

    public function getArticleByCategoriesID($language, $id, $article_url, $status) {
        $sql = "SELECT {$this->table_article}.*
                FROM {$this->table_article}, {$this->table_categories}
                WHERE {$this->table_article}.language = ?
                AND {$this->table_article}.status = ?
                AND {$this->table_article}.category_id = {$this->table_categories}.id
                AND {$this->table_categories}.friendly_url = ?
                AND {$this->table_article}.friendly_url = ?
                ORDER BY {$this->table_article}.in_order, {$this->table_article}.name ";
        return $this->query_row($sql, array($language, $status, $id, $article_url));
    }

    //for Admin -------------------------------------------------------------------------------------------

    public function getArticleByUrl($language, $article_url) {
        $sql = "SELECT {$this->table_article}.*
FROM {$this->table_article}
WHERE {$this->table_article}.language = ?
AND {$this->table_article}.friendly_url = ?";

        return $this->query_row($sql, array($language, $article_url));
    }

    public function getAllArticleByCategoriesIDAd($language, $id) {
        $sql = "SELECT *
FROM {$this->table_article}
WHERE language = ?
AND status NOT LIKE 'delete'
AND category_id = ?
ORDER BY in_order, name ";
        return $this->query_array($sql, array($language, $id));
    }

    public function updateNew($id, $data) {
        $where = "id = {$id}";
        $sql = $this->db->update_string($this->table_articles, $data, $where);
        return $this->db->query($sql);
    }

    public function addNew($data) {
        $this->db->insert($this->table_article, $data);
        return $this->db->insert_id();
    }

    public function delete($id) {
        $data = array(
            'status' => 'delete'
        );
        $where = "id = {$id}";
        $sql = $this->db->update_string($this->table_article, $data, $where);
        return $this->db->query($sql);
    }

}
